#include <stdio.h>
#include <stdint.h>
#include <time.h>

#include "../dpu/dpu.h"
// #include "lcd.h"
// #include "drv_dpu.h"
#include "drv_isp.h"

#define ISP_RAW_ADDR        MM_MIPI2DMA_BUF1
#define ISP_IR_RAW_ADDR 	0x3100000

EM_LCD_TYPE lcd_type;

// void test_ir_lcd()
// {
//     int ret;
//     dpu_layer_cfg_t layer_cfg;

// 	isp_stop(1);//stop rgb
// #if 0	
//     ret = lcd_init(MIPI_LCD_360_55);
//     //ret = lcd_init(MIPI_LCD_360_8);
//     if (ret) {
//         printf("mipi_lcd_init() err.\r\n");
//         return;
//     }

//     ret = dpu_init(MIPI_LCD_360_55);
//     //ret = dpu_init(MIPI_LCD_360_8);
//     if (ret) {
//         printf("dpu_init() err.\r\n");
//         return;
//     }   
// #endif 

// 	layer_cfg.resx = 360,
// 	 layer_cfg.resy = 240,
// 	 layer_cfg.colormode = DPU_COLORMODE_YUV420P,
// 	 layer_cfg.blendmode = DPU_BLENDMODE_SRC,
// 	 layer_cfg.startx = 0,
// 	 layer_cfg.starty = 0,
// 	 layer_cfg.sizex = 360,
// 	 layer_cfg.sizey = 240,
// 	 layer_cfg.alpha = 0xff,
// 	 layer_cfg.baseaddr = (void *)ISP_IR_RAW_ADDR + 640*60 + 140; 
// 	 layer_cfg.stride = DPU_CFG_STRIDE(640);
// 	 ret = dpu_layer_config(0, &layer_cfg);
// 	 if (ret) {
// 		 printf("dpu_layer_cfg() err.\r\n");
// 		 return;
// 	 }

// 	 layer_cfg.resx = 640,
// 	 layer_cfg.resy = 360,
// 	 layer_cfg.colormode = DPU_COLORMODE_YUV420SP,
// 	 layer_cfg.blendmode = DPU_BLENDMODE_SRC,
// 	 layer_cfg.startx = 0,
// 	 layer_cfg.starty = 360*1,
// 	 layer_cfg.sizex = 640,
// 	 layer_cfg.sizey = 360,
// 	 layer_cfg.alpha = 0xff,
// 	 layer_cfg.baseaddr = (void *)ISP_IR_RAW_ADDR;
// 	 layer_cfg.stride = -1;
// 	 ret = dpu_layer_config(1, &layer_cfg);
// 	 if (ret) {
// 		 printf("dpu_layer_cfg() err.\r\n");
// 		 return;
// 	 }

// 	 layer_cfg.resx = 640,
// 	 layer_cfg.resy = 360,
// 	 layer_cfg.colormode = DPU_COLORMODE_YUV420SP,
// 	 layer_cfg.blendmode = DPU_BLENDMODE_SRC,
// 	 layer_cfg.startx = 0,
// 	 layer_cfg.starty = 360*2,
// 	 layer_cfg.sizex = 640,
// 	 layer_cfg.sizey = 360,
// 	 layer_cfg.alpha = 0xff,
// 	 layer_cfg.baseaddr = (void *)ISP_IR_RAW_ADDR;
// 	 layer_cfg.stride = -1;
// 	 ret = dpu_layer_config(2, &layer_cfg);
// 	 if (ret) {
// 		 printf("dpu_layer_cfg() err.\r\n");
// 		 return;
// 	 }

// 	 aos_msleep(3000);
// 	 lcd_backlight(0);
// 	 aos_msleep(3000);
// 	 lcd_backlight(512);
// 	 aos_msleep(3000);
// 	 lcd_backlight(2047);
	 


// }

void rgb_camera_2_dpu_on(void)
{
    int ret;
    dpu_layer_cfg_t layer_cfg;
#if 0
    layer_cfg.resx = 640,
    layer_cfg.resy = 360,
    layer_cfg.colormode = DPU_COLORMODE_ARGB8888,
    layer_cfg.blendmode = DPU_BLENDMODE_SRC,
    layer_cfg.startx = 0,
    layer_cfg.starty = 0,
    layer_cfg.sizex = 640,
    layer_cfg.sizey = 360,
    layer_cfg.alpha = 0xff,
    layer_cfg.baseaddr = (void *)ISP_RAW_ADDR;
#endif
    layer_cfg.resx = 360,
    layer_cfg.resy = 240,
    layer_cfg.colormode = DPU_COLORMODE_ARGB8888,
    layer_cfg.blendmode = DPU_BLENDMODE_SRC,
    layer_cfg.startx = 0,
    layer_cfg.starty = 0,
    layer_cfg.sizex = 360,
    layer_cfg.sizey = 240,
    layer_cfg.alpha = 0xff,
    layer_cfg.baseaddr = (void *)ISP_RAW_ADDR + 640*60*4 + 140*4; 
    layer_cfg.stride = DPU_CFG_STRIDE(640*4);
    //ret = dpu_layer_config(0, &layer_cfg);
    // if (ret) {
    //     printf("dpu_layer_cfg() err.\r\n");
    //     return;
    // }

    layer_cfg.resx = ISP_OUTPUT_WIDTH,
    layer_cfg.resy = ISP_OUTPUT_HIGHT,
    layer_cfg.colormode = DPU_COLORMODE_ARGB8888,
    layer_cfg.blendmode = DPU_BLENDMODE_SRC,
    layer_cfg.startx = 0,
    layer_cfg.starty = 0,
#if defined(MIPI_LCD_360_55_INCH)
	layer_cfg.sizex = 720,
	layer_cfg.startx = (720-540)/2,
#elif defined(MIPI_LCD_360_8_INCH)
	layer_cfg.sizex = 800,
	layer_cfg.startx = (800-540)/2,
#else
	layer_cfg.sizex = 720,
#endif

    layer_cfg.alpha = 0xff,
    layer_cfg.baseaddr = (void *)ISP_RAW_ADDR;    
    layer_cfg.stride = -1;

    if(lcd_type == MIPI_LCD_360_55){
        layer_cfg.startx = 0;
        layer_cfg.starty = 0;
        layer_cfg.sizex = 720;
        layer_cfg.sizey = 1280;

     }else if(lcd_type == MIPI_LCD_360_8){
            layer_cfg.startx = 0;
            layer_cfg.starty = 0;
            layer_cfg.sizex = 800;
            layer_cfg.sizey = 1280;

      }

    
#ifdef DPULAYER_USECASE0
    ret = dpu_layer_config(0, &layer_cfg);
#else
    ret = dpu_layer_config(1, &layer_cfg);
#endif
    if (ret) {
        printf("dpu_layer_cfg() err.\r\n");
        return;
    }

    layer_cfg.resx = 640,
    layer_cfg.resy = 360,
    layer_cfg.colormode = DPU_COLORMODE_ARGB8888,
    layer_cfg.blendmode = DPU_BLENDMODE_SRC,
    layer_cfg.startx = 0,
    layer_cfg.starty = 360*2,
    layer_cfg.sizex = 640,
    layer_cfg.sizey = 360,
    layer_cfg.alpha = 0xff,
    layer_cfg.baseaddr = (void *)ISP_RAW_ADDR;
    layer_cfg.stride = -1;
    //ret = dpu_layer_config(2, &layer_cfg);
    if (ret) {
        printf("dpu_layer_cfg() err.\r\n");
        return;
    }
}

void vo_init(void)
{
    int ret = 0;
	//EM_LCD_TYPE lcd_type;
	
	ret = lcd_init(&lcd_type);
    if (ret) {
        printf("mipi_lcd_init() err.\r\n");
        return;
    }

    ret = dpu_init(lcd_type);
    if (ret) {
        printf("dpu_init() err.\r\n");
        return;
    }   

	rgb_camera_2_dpu_on();

#if 0//used for debug backlight
    aos_msleep(3000);
    lcd_backlight(0);
    aos_msleep(3000);
    lcd_backlight(512);
    aos_msleep(3000);
    lcd_backlight(2047);
#endif 	
    
}




// void test_ir_mipi_lcd(void)
// {
//     int ret;
//     dpu_layer_cfg_t layer_cfg;
// //	EM_LCD_TYPE lcd_type;

// 	ret = lcd_init(&lcd_type);
//     if (ret) {
//         printf("mipi_lcd_init() err.\r\n");
//         return;
//     }

//     ret = dpu_init(lcd_type);
//     if (ret) {
//         printf("dpu_init() err.\r\n");
//         return;
//     }   
// #if 0
//     layer_cfg.resx = 640,
//     layer_cfg.resy = 360,
//     layer_cfg.colormode = DPU_COLORMODE_ARGB8888,
//     layer_cfg.blendmode = DPU_BLENDMODE_SRC,
//     layer_cfg.startx = 0,
//     layer_cfg.starty = 0,
//     layer_cfg.sizex = 640,
//     layer_cfg.sizey = 360,
//     layer_cfg.alpha = 0xff,
//     layer_cfg.baseaddr = (void *)ISP_RAW_ADDR;
// #endif
//     layer_cfg.resx = 360,
//     layer_cfg.resy = 240,
//     layer_cfg.colormode = DPU_COLORMODE_ARGB8888,
//     layer_cfg.blendmode = DPU_BLENDMODE_SRC,
//     layer_cfg.startx = 0,
//     layer_cfg.starty = 0,
//     layer_cfg.sizex = 360,
//     layer_cfg.sizey = 240,
//     layer_cfg.alpha = 0xff,
//     layer_cfg.baseaddr = (void *)ISP_RAW_ADDR + 640*60*4 + 140*4; 
//     layer_cfg.stride = DPU_CFG_STRIDE(640*4);
//     //ret = dpu_layer_config(0, &layer_cfg);
//     if (ret) {
//         printf("dpu_layer_cfg() err.\r\n");
//         return;
//     }

//     layer_cfg.resx = 360,
//     layer_cfg.resy = 640,
//     layer_cfg.colormode = DPU_COLORMODE_L8,
//     layer_cfg.blendmode = DPU_BLENDMODE_SRC,
//     layer_cfg.startx = 0,
//     layer_cfg.starty = 0,
// #if defined(MIPI_LCD_360_55_INCH)
// 	layer_cfg.sizex = 720,//360,
// #elif defined(MIPI_LCD_360_8_INCH)
// 	layer_cfg.sizex = 800,//360,
// #else
// 	layer_cfg.sizex = 720,//360,
// #endif
//     layer_cfg.sizey = 1280,//640,
//     layer_cfg.alpha = 0xff,
//     layer_cfg.baseaddr = (void *)0x06200000;
//     layer_cfg.stride = -1;
//     ret = dpu_layer_config(0, &layer_cfg);
//     if (ret) {
//         printf("dpu_layer_cfg() err.\r\n");
//         return;
//     }

//     layer_cfg.resx = 640,
//     layer_cfg.resy = 360,
//     layer_cfg.colormode = DPU_COLORMODE_ARGB8888,
//     layer_cfg.blendmode = DPU_BLENDMODE_SRC,
//     layer_cfg.startx = 0,
//     layer_cfg.starty = 360*2,
//     layer_cfg.sizex = 640,
//     layer_cfg.sizey = 360,
//     layer_cfg.alpha = 0xff,
//     layer_cfg.baseaddr = (void *)ISP_RAW_ADDR;
//     layer_cfg.stride = -1;
//     //ret = dpu_layer_config(2, &layer_cfg);
//     if (ret) {
//         printf("dpu_layer_cfg() err.\r\n");
//         return;
//     }

// #if 0//used for debug backlight
//     aos_msleep(3000);
//     lcd_backlight(0);
//     aos_msleep(3000);
//     lcd_backlight(512);
//     aos_msleep(3000);
//     lcd_backlight(2047);
// #endif 	
    
// }

